<?php
/**
 * alltosun.com  message.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-1-25 下午3:55:29 $
 * $Id$
 */
class message_widget
{

    /**
     * 市场人员签约门店的短信和推送
     * @param unknown_type $store_info
     * @param unknown_type $market_info
     * @param unknown_type $agency_info
     */
    public function market_create_store_message($store_info, $market_info, $agency_info)
    {
//         if (ONDEV) {
//             return false;
//         }
        
        // 店铺
        $store_title      = $store_info['store_title'];
        $store_user_name  = $store_info['user_name'];
        $store_passowrd   = $store_info['passowrd'];
        $mobile           = $store_info['mobile'];

        // 市场人员
        $market_user_name = $market_info['market_user_name'];
        $market_user_id   = $market_info['market_user_id'];
        $market_mobile    = $market_info['market_mobile'];

        // 合伙人
        $agency_mobile    = $agency_info['agency_mobile'];
        $agency_id        = $agency_info['agency_id'];
        $agency_user_name = $agency_info['user_name'];

        // 发给店长
        $replace_arr = array('store_name' => $store_title, 'user_name' => $store_user_name, 'code' => $store_passowrd);
        aliysms_helper::sms_send('抱抱堂', $replace_arr, $mobile, 'store_sign');

        if ($market_user_id != $agency_id ) {

            // 短信通，知合伙人
            $replace_arr = array('store_name' => $store_title, 'name' => $market_user_name);
            aliysms_helper::sms_send('抱抱堂', $replace_arr, $agency_mobile, 'marketing_sign_dealer');

            // 站内信，通知合伙人
            message_center_helper::get_config_add_message('marketing_sign_dealer', $replace_arr, $agency_id, 'manage', '', 0, 'arrival');

            // 推送，通知合伙人


            // 短信，通知发市场人员
            $replace_arr = array('store_name' => $store_title, 'name' => $market_user_name);
            aliysms_helper::sms_send('抱抱堂', $replace_arr, $market_mobile, 'sign_store');

            // 站内信，通知市场人员
            message_center_helper::get_config_add_message('sign_store', $replace_arr, $market_user_id, 'manage', '', 0, 'arrival');

            // 推送，通知市场人员
        } else {
            // 合伙人自己签约，只通知自己和商户

            $replace_arr = array('store_name' => $store_title, 'name' => $agency_user_name);
            aliysms_helper::sms_send('抱抱堂', $replace_arr, $agency_mobile, 'sign_store');

            // 站内信
            message_center_helper::get_config_add_message('sign_store', $replace_arr, $agency_id, 'manage', '', 0, 'arrival');

            // 通知
        }

    }

    /**
     * 撤店短信 
     * @author gaoa@alltosun.com
     * 思路:
     * 市场人员撤店:合伙人,商家
     * 合伙人撤店: 合伙人,商家(如果有市场人员也发给市场人员)
     */
    public function stop_store_message($cur_user_info, $store_info)
    {
//         if (ONDEV) {
//             return false;
//         }
        
        if (!$cur_user_info || !$store_info) {
            return false;
        }
        
        $role_id = user_helper::get_user_role_id($cur_user_info['id']);
        $store_name = $store_info['title'].$store_info['title_branch'];
        
        $owner_info      = array();
        $dealer_info     = array();
        $marketing_info  = array();
        $owner_info      = user_helper::get_user_info($store_info['owner_id']);
        $store_user_info = user_helper::get_user_info($store_info['user_id']);
        $marketing_info = user_helper::get_user_info($store_info['marketing_id']);
        $msg_arr = array(
            'store_name' => $store_name
        );
        
        if ($store_info['agency_id']) {
            $dealer_info = user_helper::get_user_info($store_info['agency_id']);
            $msg_arr['name']      = $dealer_info['realname'];
            $msg_arr['dealer_phone_num'] = $dealer_info['mobile'];
            $msg_arr['phone_num']        = $dealer_info['mobile'];
            
            if ($store_info['agency_id'] != $store_info['marketing_id'] && $role_id != user_config::AGENCY) {
                
                $msg_arr['name']      = $marketing_info['realname'];
                $msg_arr['phone_num'] = $marketing_info['mobile'];
                $msg_arr['marketing_num'] = $marketing_info['mobile'];
            }
        }
        
        if (user_config::MARKETING == $role_id) {
            // 发送给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_info['mobile'], 'marketing_stop_store_dealer');
            message_center_helper::get_config_add_message('marketing_stop_store_dealer', $msg_arr, $dealer_info['id'], 'manage', '', 0, 'arrival');
            
            // 发给分店商家
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_info['mobile'], 'marketing_stop_store_store');
            message_center_helper::get_config_add_message('marketing_stop_store_store', $msg_arr, $store_user_info['id'], 'store', '', 0, 'arrival');
            
        } else if (user_config::AGENCY == $role_id) {
            
            // 发给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_info['mobile'], 'dealer_stop_store_dealer');
            message_center_helper::get_config_add_message('dealer_stop_store_dealer', $msg_arr, $dealer_info['id'], 'manage', '', 0, 'arrival');
            
            // 发给商家
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_info['mobile'], 'dealer_stop_store_store');
            message_center_helper::get_config_add_message('dealer_stop_store_store', $msg_arr, $store_user_info['id'], 'store', '', 0, 'arrival');
            
            // 如果有市场人员发给市场人员
            if ($marketing_info) {
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $marketing_info['mobile'], 'dealer_stop_store_marketing');
                message_center_helper::get_config_add_message('dealer_stop_store_marketing', $msg_arr, $marketing_info['id'], 'manage', '', 0, 'arrival');
            }
            
        }
        
        
    }
    
    
    /**
     * 恢复门店短信
     * @author 高昂
     * 思路:
     * 市场人员恢复门店:合伙人,商家
     * 合伙人恢复门店: 合伙人,商家(如果有市场人员也发给市场人员)
     */
    public function recover_store_message($cur_user_info, $store_info)
    {
//         if (ONDEV) {
//             return false;
//         }
        
        if (!$cur_user_info || !$store_info) {
            return false;
        }
    
        $role_id = user_helper::get_user_role_id($cur_user_info['id']);
        $store_name = $store_info['title'].$store_info['title_branch'];
    
        $owner_info      = array();
        $dealer_info     = array();
        $marketing_info  = array();
        $owner_info      = user_helper::get_user_info($store_info['owner_id']);
        $store_user_info = user_helper::get_user_info($store_info['user_id']);
        $marketing_info = user_helper::get_user_info($store_info['marketing_id']);
        $msg_arr = array(
            'store_name' => $store_name
        );
    
        if ($store_info['agency_id']) {
            $dealer_info = user_helper::get_user_info($store_info['agency_id']);
            $msg_arr['name']      = $dealer_info['realname'];
            $msg_arr['dealer_phone_num'] = $dealer_info['mobile'];
            $msg_arr['phone_num']        = $dealer_info['mobile'];
    
            if ($store_info['agency_id'] != $store_info['marketing_id'] && $role_id != user_config::AGENCY) {
                $msg_arr['name']      = $marketing_info['realname'];
                $msg_arr['marketing'] = $marketing_info['realname'];
                $msg_arr['phone_num'] = $marketing_info['mobile'];
            }
        }
    
        if (user_config::MARKETING == $role_id) {
            // 发送给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_info['mobile'], 'marketing_recover_store_dealer');
            message_center_helper::get_config_add_message('marketing_recover_store_dealer', $msg_arr, $dealer_info['id'], 'manage', '', 0, 'arrival');
    
            // 发给商家
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_info['mobile'], 'marketing_recover_store_store');
            message_center_helper::get_config_add_message('marketing_recover_store_store', $msg_arr, $store_user_info['id'], 'store', '', 0, 'arrival');
    
        } else if (user_config::AGENCY == $role_id) {
    
            // 发给合伙人
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $dealer_info['mobile'], 'dealer_recover_store_dealer');
            message_center_helper::get_config_add_message('dealer_recover_store_dealer', $msg_arr, $dealer_info['id'], 'manage', '', 0, 'arrival');
    
            // 发给商家
            aliysms_helper::sms_send('抱抱堂', $msg_arr, $store_user_info['mobile'], 'dealer_recover_store_store');
            message_center_helper::get_config_add_message('dealer_recover_store_store', $msg_arr, $store_user_info['id'], 'store', '', 0, 'arrival');
            // 如果有市场人员发给市场人员
            if ($marketing_info) {
                aliysms_helper::sms_send('抱抱堂', $msg_arr, $marketing_info['mobile'], 'dealer_recover_store_marketing');
                message_center_helper::get_config_add_message('dealer_recover_store_marketing', $msg_arr, $marketing_info['id'], 'manage', '', 0, 'arrival');
            }
    
        }
    
    
    }


}